package main.components;

import main.objects.SmetNode;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class RenameSmetDialog extends JDialog {

    private SmetNode smet;
    private JTextField inputSmetNum, inputSmetName;

    public RenameSmetDialog(SmetNode smet) {

        this.smet = smet;
        initDialog();

    }

    private void initDialog() {

        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLayout(new GridBagLayout());
        setResizable(false);
        setType(Type.UTILITY);
        setModal(true);
        setTitle("Переименование сметы");

        add(new JLabel("Номер сметы:"), new GridBagConstraints(0, 0, 1, 1, 0, 0.1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(30, 30, 0, 0), 0, 0));
        add(new JLabel("Наименование сметы:"), new GridBagConstraints(1, 0, 1, 1, 0, 0.1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(30, 10, 0, 0), 0, 0));

        inputSmetNum = new JTextField(smet.getNum());
        inputSmetNum.setPreferredSize(new Dimension(260, 20));
        inputSmetName = new JTextField(smet.getName());
        inputSmetName.setPreferredSize(new Dimension(550, 20));
        inputSmetNum.addKeyListener(new KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                    inputSmetName.requestFocus();
                }
            }
        });
        inputSmetName.addKeyListener(new KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                    renameSmet();
                }
            }
        });


        add(inputSmetNum, new GridBagConstraints(0, 1, 1, 1, 0, 0.1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 30, 0, 0), 0, 0));
        add(inputSmetName, new GridBagConstraints(1, 1, 1, 1, 0, 0.1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 10, 10, 30), 0, 0));

        JButton btnSave = new JButton("Сохранить");
        btnSave.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                renameSmet();
            }
        });

        JButton btnCancel = new JButton("Отмена");
        btnCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();
            }
        });

        add(btnSave, new GridBagConstraints(1, 2, 1, 1, 0, 0.1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 110), 0, 0));
        add(btnCancel, new GridBagConstraints(1, 2, 1, 1, 0, 0.1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 50, 30, 30), 0, 0));

        pack();
        setLocationRelativeTo(null);
        setVisible(true);

    }

    private void renameSmet() {

        smet.setName(inputSmetName.getText());
        smet.setNum(inputSmetNum.getText());
        smet.updateSmetName();
        dispose();

    }


}
